home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power Programmierung
/
Power-Programmierung (Tewi)(1994).iso
/
magazine
/
aijournl
/
1987_03
/
actor.mar
< prev
next >
Wrap
Text File
|
1987-02-19
|
3KB
|
74 lines
"Actor Goes on Stage"
by Andrew P. Bernat
March 1987 AI EXPERT
Listing 1.
A Comparison of ACTOR and Smalltalk-80
/* ACTOR solution to Towers of Hanoi */
/* Create a new class named TowerOfHanoi to handle the problem.
The new class is directly beneath Object in the hierarchy;
Object being at the top of the class hierarchy.*/
inherit(Object, #TowerOfHanoi, nil, nil, nil);
/* Here we set the compiler's curClass instance variable,
which specifies which class the following messages are to
apply to. */
Compiler.curClass := TowerOfHanoi;
/* Define a new message named moveTower */
Def moveTower(self, height, from, to, use)
{ if height > 0
then moveTower(self, height - 1, from, use, to);
moveDisk(self, from, to);
moveTower(self, height - 1, use, to, from);
endif;
}
/* Define a new message named moveDisk */
Def moveDisk(self, from, to)
{ eol(ThePort);
print(from); print("->"); print(to);
}
/* Add a new member of class Object to the system dictionary,
which is also named Actor, at key location sample */
Actor[#sample] := new(Object);
/* Send message moveTower to the object sample. sample, being
of class Object, will respond using the code defined above.
The parameters say to use three disks, and move them from
tower 1 to tower 3 using tower 2 */
moveTower(sample, 3, 1, 3, 2);
" Tower of Hanoi code as implemented in Smalltalk/V. Note that
these messages and methods would be implemented directly
through the System Browser. "
" Declare a new sublcass of Object."
Object subclass: #TowerOfHanoi
" Instance variables would be declared here, but none are
needed. "
TowerOfHanoi methods
" Declare the moveTower message. "
moveTower: height
from: ftower to: ttower using: utower
(> height 0)
ifTrue: [self moveTower: height - 1
from: ftower to: utower using: ttower.
self moveDisk: ftower to: ttower.
self moveTower: height - 1
from: utower to: ttower using: ftower]
" Declare the moveDisk message. Transcript is a the standard
output window."
moveDisk: ftower to: ttower
Transcript cr.
Transcript show: ftower.
Transcript show: '->'.
Transcript show: ttower.
" To execute, evaluate
(new TowerOfHanoi) moveTower: 3 from: 1 to: 3 using: 2"
r.
" To execute, evaluate
(new TowerOfHanoi) moveTower: 3 from: 1 to: 3 using: 2"